package com.east.controller.department;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.east.bean.Department;
import com.east.bean.DictionaryItems;
import com.east.bean.Staff;
import com.east.service.department.DepartmentService;
import com.east.service.staff.StaffService;
import com.east.utils.PageJson;
import com.east.utils.Pagenation;
import com.east.utils.Result;
import com.east.utils.ResultUtil;
import com.github.pagehelper.Page;

@RestController
@RequestMapping("department")
public class DepartmentController {
		@Autowired
		DepartmentService departmentService;
		@Autowired
		StaffService staffService;
		/**
		 * 岗位查询
		 * @param pagenation
		 * @param key
		 * @return
		 */
		@GetMapping("all")
		@RequiresPermissions("department:all")
		public Result findAll(){
			List<DictionaryItems> findTree = departmentService.findAll();
			return ResultUtil.success(findTree);
		}
		
		/**
		 * 组织架构树
		 * @param pagenation
		 * @param key
		 * @return
		 */
		@GetMapping("tree")
		@RequiresPermissions("department:tree")
		public Result findtree(){
			List<DepartmentTree> findTree = departmentService.findTree();
			return ResultUtil.success(findTree);
		}
		/**
		 * 详情
		 * @return
		 */
		@GetMapping("findOne")
		@RequiresPermissions("department:findOne")
		public Result findOneDepartment(Department department){
			Department findByDepartment = departmentService.findByDepartment(department);
			return ResultUtil.success(findByDepartment);
		}
		/**
		 * 部门下的员工
		 * @param pagenation
		 * @param dId
		 * @return
		 */
		@GetMapping("findstaff")
		@RequiresPermissions("department:findstaff")
		public Result findOneDepartmentStaff(Pagenation pagenation,Integer dId){
			 Page<Staff> findByDepartMentId = staffService.findByDepartMentId(pagenation, dId);
			 Object pageJson = new PageJson<Staff>().pageJson(findByDepartMentId);
			return ResultUtil.success(pageJson);
		}
		/**
		 * 保存
		 * @return
		 */
		@PostMapping("save")
		@RequiresPermissions("department:save")
		public Result saveDepartment(Department department){
			int findTree = departmentService.saveDepartMent(department);
			if(findTree>0) {
				return ResultUtil.success("保存成功");
			}else {
				return ResultUtil.success("保存失败");
			}
		}
		/**
		 * 修改
		 * @return
		 */
		@PostMapping("update")
		@RequiresPermissions("department:update")
		public Result updateDepartment(Department department){
			int findTree = departmentService.updateDepartMent(department);
			if(findTree>0) {
				return ResultUtil.success("修改成功");
			}else {
				return ResultUtil.success("修改失败");
			}
		}
		/**
		 * 删除
		 * @return
		 */
		@DeleteMapping("del")
		@RequiresPermissions("department:del")
		public Result delDepartment(Department department){
			int findTree = departmentService.delDepartMent(department);
			if(findTree>0) {
				return ResultUtil.success("删除成功");
			}else {
				return ResultUtil.success("删除失败");
			}
		}
		
		
}
